/*
 * @Author: liujm
 * @Date: 2022-12-02 10:45:01
 * @Description: file content
 */
import { groupBy } from 'lodash-es';
import { mainSend } from '@/api/utils/constants';
import GswjParam from '../entities/GswjParam';
import CommonService from '../../common/CommonService';
import GswjResult from '../entities/GswjResult';

/**
 * 签报service
 */

class GswjService extends CommonService {
  /**
   * 获取表单信息，并且格式化表单内容
   * @param {} piid
   */
  getFormInfo = async (piid, obj) => {
    const data = await this.getFormCommonInfo(piid);
    const gswgResult = new GswjResult(data, obj);
    return gswgResult.getData();
  }

  /**
   * 保存表单信息
   * @param {*} data
   * @param {*} files
   */
  saveFormInfo = async (data, files = [], isSaveFile = true) => {
    const gswjParam = new GswjParam(data, files, isSaveFile);
    const { data: { data: id } } = await this.saveFormCommonInfo(gswjParam.setData());
    return id;
  }

  /**
   * 获取主送抄送数据
   * @param {string} processDefinition 流程模板code
   */
  getSendData = async (processDefinition = 'dwwj', gatewayData = true, isShowLeader = false) => {
    const data = await this.getSendCommonData(processDefinition, gatewayData);
    const arrs = groupBy(data, 'name');
    // 主抄送数据
    const res = [{
      1: this.formatSendData(arrs[mainSend.SGS], 1, 1, 4),
      2: this.formatSendData(arrs[mainSend.JWGS], 1, 2, 2),
      3: this.formatSendData(arrs[mainSend.JTZBBM], 1, 3, 2),
      4: this.formatSendData(arrs[mainSend.ZSDW], 1, 4, 2),
      5: this.formatSendData(arrs[mainSend.BM], 1, 5, 4),
    }, {}, {
      2: this.formatSendData(arrs[mainSend.ZYGS], 3, 2, 2),
      3: this.formatSendData(arrs[mainSend.JJJCZ], 3, 3, 1),
      4: this.formatSendData(arrs[mainSend.QT], 3, 4, 2),
    }, {
      3: this.formatSendData(arrs[mainSend.JTGH], 4, 3, 1),
    }];
    // 发送数据
    const sendData = [{
      1: this.formatSendData(arrs[mainSend.LD], 1, 1, 2),
      2: this.formatSendData(arrs[mainSend.JWGS], 1, 2, 2),
      3: this.formatSendData(arrs[mainSend.JTZBBM], 1, 3, 2),
      4: this.formatSendData(arrs[mainSend.ZSDW], 1, 4, 2),
      5: this.formatSendData(arrs[mainSend.BM], 1, 5, 4),
    }, {}, {
      1: this.formatSendData(arrs[mainSend.SGS], 1, 1, 2),
      2: this.formatSendData(arrs[mainSend.ZYGS], 3, 2, 2),
      3: this.formatSendData(arrs[mainSend.JJJCZ], 3, 3, 1),
      4: this.formatSendData(arrs[mainSend.QT], 3, 4, 2),
    }, {
      3: this.formatSendData(arrs[mainSend.JTGH], 4, 3, 1),
    }];
    return {
      data: isShowLeader ? sendData : res,
      // 每行列数
      cols: 5,
      // 行数
      rows: 4,
    };
  }
}

export default GswjService;
